package com.example.aboutspringboot.controller;

import com.example.aboutspringboot.entity.Order;
import com.example.aboutspringboot.entity.OrderItem;
import com.example.aboutspringboot.entity.ShopCar;
import com.example.aboutspringboot.mapper.OrderMapper;
import com.example.aboutspringboot.service.OrderService;
import com.example.aboutspringboot.service.ShopCarService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("order")
@CrossOrigin//跨域
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private ShopCarService shopCarService;
    //定义时间的格式为 年-月-日-时:分:秒 不要有空格，编译可能过不去
    private final SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");

    //加载所有订单
    @GetMapping("/selectOrder")
    Map<String,Object> selectOrderAll(){
        List<Order> list=orderService.selectOrderAll();
        Map<String, Object> map = new LinkedHashMap<>();
        for(Order order: list){
            order.setOrderItems(orderService.selectOrderItem(order.getId()));
            double allPrice=0.0;
            for (OrderItem orderItem: order.getOrderItems()){
               allPrice=allPrice+ orderItem.getNumber()*orderItem.getPrice(); //计算出总价
            }
            order.setAll(allPrice);
        }
        map.put("order",list);
        return map;
    }

}
